//
//  AsViewController.m
//  AsSDKDemo
//
//  Created by i4.cn\STM on 14-6-16.
//  Copyright (c) 2014年 i4.cn. All rights reserved.
//

#import "AsViewController.h"

@interface AsViewController ()

@end

@implementation AsViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self.view setBackgroundColor:[UIColor whiteColor]];
    [[AsPlatformSDK sharedInstance] setDelegate:self];
    NSArray *nameArray = [[NSArray alloc] initWithObjects:@"登陆",@"用户中心",@"注销", nil];
    NSArray *payNameArray = [[NSArray alloc] initWithObjects:@"消费1元",@"消费3元",@"消费10元", nil];
    for (int i = 0; i < [nameArray count]; i++) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        [btn setTag:i + 1];
        [btn addTarget:self action:@selector(btnTouch:) forControlEvents:UIControlEventTouchUpInside];
        if (i == 0) {
            [btn setFrame:CGRectMake(10, 30, 100, 40)];
        }
        else if (i == 1)
        {
            [btn setHidden:YES];
            [btn setFrame:CGRectMake(120, 30, 100, 40)];
        }
        else if (i == 2)
        {
            [btn setHidden:YES];
            [btn setFrame:CGRectMake(230, 30, 100, 40)];
        }

        [btn setTitle:[nameArray objectAtIndex:i] forState:UIControlStateNormal];
        [self.view addSubview:btn];
    }
    
    
    for (int i = 0 ; i < [payNameArray count]; i++) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        [btn setTag:i + 10];
        [btn addTarget:self action:@selector(btnBuyTouch:) forControlEvents:UIControlEventTouchUpInside];
        if (i == 0) {
            [btn setHidden:YES];
            [btn setFrame:CGRectMake(10, 80, 100, 40)];
        }
        else if (i == 1)
        {
            [btn setHidden:YES];
            [btn setFrame:CGRectMake(120, 80, 100, 40)];
        }
        else if (i == 2){
            [btn setHidden:YES];
            [btn setFrame:CGRectMake(230, 80, 100, 40)];
        }
        
        [btn setTitle:[payNameArray objectAtIndex:i] forState:UIControlStateNormal];
        [self.view addSubview:btn];
    }
    [nameArray release];
    [payNameArray release];
    
}

- (void)btnTouch:(UIButton *)sender
{
    if (sender.tag == 1)
    {
        [[AsPlatformSDK sharedInstance] showLogin];
    }
    if (sender.tag == 2)
    {
        [[AsPlatformSDK sharedInstance] showCenter];
    }
    if (sender.tag == 3)
    {
        [[AsPlatformSDK sharedInstance] asLogout];
        [[[AsPlatformSDK sharedInstance] delegate] asLogOffCallBack];
    }
}
- (void)btnBuyTouch:(UIButton *)sender
{
    if (sender.tag == 10)
    {
        [[AsPlatformSDK sharedInstance] exchangeGoods:1 BillNo:@"123" BillTitle:@"爱思测试" RoleId:@"1" ZoneId:0];
    }
    if (sender.tag == 11)
    {
        [[AsPlatformSDK sharedInstance] exchangeGoods:3 BillNo:@"123" BillTitle:@"爱思测试" RoleId:@"1" ZoneId:0];
    }
    if (sender.tag == 12)
    {
        [[AsPlatformSDK sharedInstance] exchangeGoods:10 BillNo:@"123" BillTitle:@"爱思测试" RoleId:@"1" ZoneId:0];
    }
    
}


//-SDK 1.5.2 - 新增的支付宝支付的回调
- (void)asAlixPayResultCallBack:(int)statusCode
{
    //    * 9000     订单支付成功
    //    * 8000     正在处理
    //    * 4000     订单支付失败
    //    * 6001     用户中途取消
    //    * 6002     网络连接出错
    
    switch (statusCode) {
        case 9000:
            NSLog(@"支付宝的回调 - 订单支付成功 : %d", statusCode);
            break;
        case 8000:
            NSLog(@"支付宝的回调 - 正在处理 : %d", statusCode);
            break;
        case 4000:
            NSLog(@"支付宝的回调 - 订单支付失败 : %d", statusCode);
            break;
        case 6001:
            NSLog(@"支付宝的回调 - 用户中途取消 : %d", statusCode);
            break;
        case 6002:
            NSLog(@"支付宝的回调 - 网络连接出错 : %d", statusCode);
            break;
        default:
            break;
    }
}


//-SDK 1.5.2 - 新增的银联支付的回调
- (void)asUPPayPluginResultCallBack:(NSString *)result
{
    //    * success  支付成功
    //    * fail     支付失败
    //    * cancel   用户取消支付
    if ([result isEqualToString:@"success"]) {
        NSLog(@"银联支付的回调 - 支付成功 : %@", result);
    }
    if ([result isEqualToString:@"fail"]) {
        NSLog(@"银联支付的回调 - 支付失败 : %@", result);
    }
    if ([result isEqualToString:@"cancel"]){
        NSLog(@"银联支付的回调 - 用户取消支付 : %@", result);
    }
    
}


//-SDK 1.4.1 - 新增的关闭用户中心回调
- (void)asClosedCenterViewCallBack
{
    // 点击按钮关闭用户中心的回调
    NSLog(@"点击按钮关闭用户中心的回调");
    
}

- (void)asPayResultCallBack:(AsPayResultCode)paramPayResultCode
{
    /*
     * 特别提醒:
     * 该回调只会在账户的爱思币余额 >= 道具面额 时，才会执行；
     * 爱思币 < 道具面额 的情形下，该回调不会执行。
     */
    NSLog(@"支付结果的回调");
}

- (void)asVerifyingUpdatePassCallBack
{
    NSLog(@"检测更新的回调");

    [[AsPlatformSDK sharedInstance] showLogin];
}

- (void)asLoginCallBack:(NSString *)paramToken
{
    
    [[AsPlatformSDK sharedInstance] currentUserName];
    [[AsPlatformSDK sharedInstance] currentUserId];
    NSLog(@"登陆的回调 - %@",paramToken);
    //此URL为测试demo。请自行更改地址
    NSString *requestURLStr = [NSString stringWithFormat:@"https://pay.i4.cn/member_game.action?token=%@",paramToken];
    NSURL *requestUrl = [[NSURL alloc] initWithString:[requestURLStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:requestUrl];
    
    [request setHTTPMethod:@"POST"];
    [request setTimeoutInterval:10];
    
    NSHTTPURLResponse *response = nil;
    NSError *error = nil;
    NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
    
    NSDictionary *resultDic = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingAllowFragments error:nil];
    int status = [[resultDic objectForKey:@"status"] integerValue];
    
    //为0为正常
    if (status == 0)
    {
        NSLog(@"登陆成功");
        UIButton *btn = (UIButton *)[self.view viewWithTag:2];
        UIButton *btn1 = (UIButton *)[self.view viewWithTag:3];
        [btn setHidden:NO];
        [btn1 setHidden:NO];
        
        
        UIButton *buyBtn = (UIButton *)[self.view viewWithTag:10];
        UIButton *buyBtn1 = (UIButton *)[self.view viewWithTag:11];
        UIButton *buyBtn2 = (UIButton *)[self.view viewWithTag:12];
        [buyBtn setHidden:NO];
        [buyBtn1 setHidden:NO];
        [buyBtn2 setHidden:NO];
    }
}

- (void)asLogOffCallBack
{
    NSLog(@"注销的回掉");
    UIButton *btn = (UIButton *)[self.view viewWithTag:2];
    UIButton *btn1 = (UIButton *)[self.view viewWithTag:3];
    [btn setHidden:YES];
    [btn1 setHidden:YES];
    
    UIButton *buyBtn = (UIButton *)[self.view viewWithTag:10];
    UIButton *buyBtn1 = (UIButton *)[self.view viewWithTag:11];
    UIButton *buyBtn2 = (UIButton *)[self.view viewWithTag:12];
    [buyBtn setHidden:YES];
    [buyBtn1 setHidden:YES];
    [buyBtn2 setHidden:YES];
    
    
    static BOOL isPad;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken,^{
        isPad = (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad);
    });
    float delay = isPad ? 1.2 : 0.6;
    [[AsPlatformSDK sharedInstance] performSelector:@selector(showLogin) withObject:nil afterDelay:delay];
    
}

- (void)asClosePageViewCallBack:(AsPageCode)paramPageCode
{
    switch (paramPageCode) {
        case AsLoginViewPageCode:
            NSLog(@"关闭页面的回调 - 登录页面关闭 - AsLoginViewPageCode : %d", paramPageCode);
            break;
        case AsRegisterViewPageCode:
            NSLog(@"关闭页面的回调 - 注册页面关闭 - AsRegisterViewPageCode : %d", paramPageCode);
            break;
        default:
            break;
    }
}


- (void)asCloseWebViewCallBack:(AsWebViewCode)paramWebViewCode
{
    switch (paramWebViewCode) {
        case AsWebViewCodeRecharge:
            NSLog(@"关闭网页的回调 - AsWebViewCodeRecharge : %d", paramWebViewCode);
            break;
        case AsWebViewCodeRechargeAndExchange:
            NSLog(@"关闭网页的回调 - AsWebViewCodeRechargeAndExchange : %d", paramWebViewCode);
        default:
            break;
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 // 设置状态栏(网络、时间、电池信息)隐藏----操作xxx-info.plist
    在xxx-info.plist 中设置
    (1)   Status bar is initially hidden   YES
    (2)   View controller-based status bar appearance   NO
 
 */


/*
 * 游戏为 横屏 时，在根控制器中，添加下面的 3个方法，限制游戏的方向为 横屏
 * 这么设置的原因在于：银联iPhone 版本，需要支持Portrait 方向，若游戏是横屏，就需要勾选 Portrait、Landscape Left、Landscape Right
   采用下面的3个方法就能起到在支持Portrait 方向的同时，不影响游戏本身的布局方向
 */
 // 设置旋转支持的方向----横屏--工程配置项中Device Orientation 勾选 Portrait、Landscape Left、Landscape Right
 // Override to allow orientations other than the default portrait orientation.
 // This method is deprecated on ios6
 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
 {
 return (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight);
 }
 // For ios6 later, use supportedInterfaceOrientations & shouldAutorotate instead
 -(NSUInteger)supportedInterfaceOrientations
 {
 return UIInterfaceOrientationMaskLandscape;
 }
 
 - (BOOL)shouldAutorotate
 {
 return YES;
 }


/*
 * 游戏为 竖屏 时，在根控制器中，添加下面的 3个方法，限制游戏的方向为 竖屏
 */
/*
 // 设置旋转支持的方向----竖屏--工程配置项中Device Orientation 勾选 Portrait、Upside Down
 // Override to allow orientations other than the default portrait orientation.
 // This method is deprecated on ios6
 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
 {
 return (toInterfaceOrientation == UIInterfaceOrientationPortrait || toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown);
 }
 // For ios6 later, use supportedInterfaceOrientations & shouldAutorotate instead
 -(NSUInteger)supportedInterfaceOrientations
 {
 return (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown);
 }
 
 - (BOOL)shouldAutorotate
 {
 return YES;
 }
 */






/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
